#import "MyCell.h"
#import "SimpleTreeNode.h"

@implementation MyCell

- (id) init
{
	self = [super init];
	if (self != nil) {
		//make it not editable
		[self setEnabled:NO];
	}
	return self;
}


- (id)copyWithZone:(NSZone*)zone {
    MyCell* newCopy = [[MyCell alloc] init];
    return newCopy;
}

#pragma mark -
#pragma mark getter and setter
- (void)setObjectValue:(id)object {
	[super setObjectValue:object];
}


- (void)drawInteriorWithFrame:(NSRect)theCellFrame inView:(NSView *)theControlView
{
	// Get the simpleNodeData
	FXTreeNode* item = (FXTreeNode*)[self objectValue];

	// Inset the cell frame to give everything a little horizontal padding
	NSRect		anInsetRect = NSInsetRect(theCellFrame,0,0);
	
	// Make the icon
	int state = [[item getUserInfo] userState];
	NSImage *	anIcon = nil;
	switch(state)
	{
		case FX_STATUS_ONLINE:
			anIcon = [NSImage imageNamed:@"available"];
			break;
		case FX_STATUS_BUSY:
		case FX_STATUS_MEETING:
		case FX_STATUS_PHONE:
			anIcon = [NSImage imageNamed:@"busy"];
			break;
		case FX_STATUS_AWAY:
		case FX_STATUS_DINNER:
		case FX_STATUS_EXTENDED_AWAY:
			anIcon = [NSImage imageNamed:@"away"];
			break;
		case FX_STATUS_OFFLINE:
			anIcon = [NSImage imageNamed:@"offline"];
			break;
		case FX_STATUS_MOBILE:
			anIcon = [NSImage imageNamed:@"phone"];
			break;
		case FX_STATUS_WAITING_AUTH:
			anIcon = [NSImage imageNamed:@"waittingAuth"];
			break;
			
	}
	
	// Flip the icon because the entire cell has a flipped coordinate system
	[anIcon setFlipped:YES];
	
	// get the size of the icon for layout
	NSSize		anIconSize = [anIcon size];
	
	// Make attributes for our strings
	NSMutableParagraphStyle * aParagraphStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
	[aParagraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
	
	NSMutableDictionary * aGroupTitleAttributes = [[[NSMutableDictionary alloc] initWithObjectsAndKeys:
											   [NSColor blackColor],NSForegroundColorAttributeName,
											   [NSFont systemFontOfSize:12.0],NSFontAttributeName,
											   aParagraphStyle, NSParagraphStyleAttributeName,
											   nil] autorelease];
	// Title attributes: system font, 14pt, black, truncate tail
	NSMutableDictionary * aTitleAttributes = [[[NSMutableDictionary alloc] initWithObjectsAndKeys:
											 [NSColor blackColor],NSForegroundColorAttributeName,
											 [NSFont systemFontOfSize:12.0],NSFontAttributeName,
											 aParagraphStyle, NSParagraphStyleAttributeName,
											 nil] autorelease];
											
	// Subtitle attributes: system font, 12pt, gray, truncate tail
	NSMutableDictionary * aSubtitleAttributes = [[[NSMutableDictionary alloc] initWithObjectsAndKeys:
												[NSColor grayColor],NSForegroundColorAttributeName,
												[NSFont systemFontOfSize:11.0],NSFontAttributeName,
												aParagraphStyle, NSParagraphStyleAttributeName,
												nil] autorelease];
	float		aVerticalPadding = 5.0;
	float		aHorizontalPadding = 10.0;
	
	if ( [item isLeaf] )
	{
		NSString *	aTitle = [NSString stringWithString:[[item getUserInfo] userName]?[[item getUserInfo] userName]:@""];
		NSSize		aTitleSize = [aTitle sizeWithAttributes:aTitleAttributes];
		NSString *	aSubtitle = [NSString stringWithString:[item isGroup]?@"":[[item getUserInfo] userImpresa]];
		NSSize		aSubtitleSize = [aSubtitle sizeWithAttributes:aSubtitleAttributes];
		
		if ( [aSubtitle length] != 0 )
		{
			NSRect		anIconBox = NSMakeRect(anInsetRect.origin.x,
											   anInsetRect.origin.y + anInsetRect.size.height*.5 - anIconSize.height*.5,
											   anIconSize.width,
											   anIconSize.height);
			float		aCombinedHeight = aTitleSize.height + aSubtitleSize.height + aVerticalPadding;
		
			NSRect		aTextBox = NSMakeRect(anIconBox.origin.x + anIconSize.width + aHorizontalPadding,
											  anInsetRect.origin.y + anInsetRect.size.height*.5 - aCombinedHeight*.5,
											  anInsetRect.size.width - anIconSize.width - aHorizontalPadding,
											  aCombinedHeight);
			NSRect		aTitleBox = NSMakeRect(aTextBox.origin.x, 
											   aTextBox.origin.y + aTextBox.size.height*.5 - aTitleSize.height,
											   aTextBox.size.width,
											   aTitleSize.height);
												
			NSRect		aSubtitleBox = NSMakeRect(aTextBox.origin.x,
												  aTextBox.origin.y + aTextBox.size.height*.5,
												  aTextBox.size.width,
												  aSubtitleSize.height);
			
			
			if(	[self isHighlighted])
			{
				// if the cell is highlighted, draw the text white
				[aTitleAttributes setValue:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
				[aSubtitleAttributes setValue:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
			}
			else
			{
			// if the cell is not highlighted, draw the title black and the subtile gray
				[aTitleAttributes setValue:[NSColor blackColor] forKey:NSForegroundColorAttributeName];
				[aSubtitleAttributes setValue:[NSColor grayColor] forKey:NSForegroundColorAttributeName];
			}
		
		
			// Draw the icon
			[anIcon drawInRect:anIconBox fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
		
			// Draw the text
			[aTitle drawInRect:aTitleBox withAttributes:aTitleAttributes];
			[aSubtitle drawInRect:aSubtitleBox withAttributes:aSubtitleAttributes];
		}
		else
		{
			NSRect		anIconBox = NSMakeRect(anInsetRect.origin.x,
											   anInsetRect.origin.y + anInsetRect.size.height*.5 - anIconSize.height*.5,
											   anIconSize.width,
											   anIconSize.height);
			float		aCombinedHeight = aTitleSize.height + aSubtitleSize.height + aVerticalPadding;
			
			NSRect		aTextBox = NSMakeRect(anIconBox.origin.x + anIconSize.width + aHorizontalPadding,
											  anInsetRect.origin.y + anInsetRect.size.height*.5 - aCombinedHeight*.5,
											  anInsetRect.size.width - anIconSize.width - aHorizontalPadding,
											  aCombinedHeight);
			NSRect		aTitleBox = NSMakeRect(aTextBox.origin.x, 
											   aTextBox.origin.y + aTextBox.size.height*.5 - aTitleSize.height*.5,
											   aTextBox.size.width,
											   aTitleSize.height);
			
			
			if(	[self isHighlighted])
			{
				// if the cell is highlighted, draw the text white
				[aTitleAttributes setValue:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
			}
			else
			{
				// if the cell is not highlighted, draw the title black and the subtile gray
				[aTitleAttributes setValue:[NSColor blackColor] forKey:NSForegroundColorAttributeName];
			}
			
			
			// Draw the icon
			[anIcon drawInRect:anIconBox fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
			
			// Draw the text
			[aTitle drawInRect:aTitleBox withAttributes:aTitleAttributes];
		}
	}
	else
	{
		NSString *	aGroupTitle = [NSString stringWithString:[item getGroupName]?[item getGroupName]:@""];  
		NSSize		aGroupTitleSize = [aGroupTitle sizeWithAttributes:aGroupTitleAttributes];
		
		NSString *	groupNum = [NSString stringWithFormat:@"%i", [item numberOfChildren]];
		NSSize		aGroupNumSize = [groupNum sizeWithAttributes:aGroupTitleAttributes];
		NSRect		aGroupNumBox = NSMakeRect(anInsetRect.origin.x+theCellFrame.size.width-aGroupNumSize.width,
											anInsetRect.origin.y + anInsetRect.size.height*.5 - aGroupNumSize.height*.5,
											aGroupNumSize.width,
											aGroupNumSize.height);
		
		NSRect		aGroupTitleBox = NSMakeRect(anInsetRect.origin.x+5.0,
										   anInsetRect.origin.y + anInsetRect.size.height*.5 - aGroupTitleSize.height*.5,
										   aGroupTitleSize.width,
										   aGroupTitleSize.height);
		[aGroupTitle drawInRect:aGroupTitleBox withAttributes:aGroupTitleAttributes];
		[groupNum drawInRect:aGroupNumBox withAttributes:aGroupTitleAttributes];
	}
}


@end
